<?php  
class AppController extends Controller {
	var $components = array('Uuid', 'Cookie');
	var $helpers = array('Html', 'Crumb');
	
	// Cookies:
	function cookieMonster() {
		$this->Cookie->name = 'PHpatrol';
		$this->Cookie->time = 86400; // or '1 day'
		$this->Cookie->domain = '.'.$_SERVER['SERVER_NAME'];
		$this->Cookie->key = 'qSI232qs*&sXOw!';
		$this->set('controller', $this->name);
		$this->set('params', $this->params);
		// Note: as of 9/24/09, none of these cases seems to apply to the home page. Not sure why.
		switch ($this->name) :
			case 'Pages':
				$this->set('cookiestatus', 'This is a page!');
				// we are dealing with a help page or something other than the home page:
				if(constant('CLIENT_NAME') != 'home') :
					$pageinfo = $this->Page->fetchPageInfoByName($this->page);
					$this->Cookie->write('page', $pageinfo, true, '+1 hour');
					$this->set('cookiestatus', 'This is a page!');
				else :
					$this->Cookie->write('page', 'home', true, '+1 hour');
					$this->set('cookiestatus', 'This is home, sweet home!');
				endif;
				break;
			case 'Users':
				$userinfo = $this->User->fetchUserInfoByName($this->params['pass'][0]);
				$this->Cookie->write('user', $userinfo, true, '+1 hour');
				$this->set('cookiestatus', 'This is a user!');
				break;
			case 'Metros':
				$metroinfo = $this->Metro->fetchMetroInfoByName($this->params['pass'][0]);
				$this->Cookie->write('metro', $metroinfo, true, '+1 hour');
				$this->set('cookiestatus', 'This is a metro!');
				break;
			case 'Towns':
				$towninfo = $this->Town->fetchTownInfoByName($this->params['town-name']);
				$this->Cookie->write('town', $towninfo, true, '+1 hour');
				$this->set('cookiestatus', 'This is a town!');
				break;
			case 'Potholes':
				$potholeinfo = $this->Pothole->fetchPotholeInfoByName($this->params['pass'][0]);
				$this->Cookie->write('pothole', $potholeinfo, true, '+1 hour');
				$this->set('cookiestatus', 'This is a pothole!');
				break;
			default:
				$this->set('cookiestatus', 'This is a mystery! '.$this->name);
				break;
		endswitch;
	}
	
	
	/**
	 * Returns a lower case string with all spaces converted to $replacement and non word characters removed.
	 *
	 * @param string $string
	 * @param string $replacement
	 * @param int $length  max string length
	 * @return string
	 * @access public
	 */
	function makeSlug($string, $length = 40, $replacement = '-') {
		$string = strtolower(Inflector::slug($string, $replacement));
			if (strlen($string) > $length) {
			   $string = substr($string, 0, $length);
			}
			if (substr($string, -1) == '-') {
				$string = substr($string, 0, $length-1);
			}
		return $string;
	}
}

?>